package com.dvdfe8.crochet.modelo.dao;

import com.dvdfe8.crochet.modelo.entity.Linea;
import com.dvdfe8.crochet.modelo.entity.Pedido;
import com.dvdfe8.crochet.modelo.service.PedidoService;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class LineaDaoImpl implements LineaDao {

    @Autowired
    SessionFactory sessionFactory;
    
    @Override
    @Transactional
    public void crear(Linea linea) {
        Pedido pedido = linea.getPedido();
        pedido.setTotal(pedido.getTotal()+linea.getAmigurumi().getPrecio());
        pedido.setNumMunecos(pedido.getNumMunecos()+1);
        sessionFactory.getCurrentSession().saveOrUpdate(linea);        
    }

    @Override
    @Transactional
    public void eliminar(Linea linea) {
        sessionFactory.getCurrentSession().delete(linea);
    }

    @Override
    @Transactional
    public Linea getLineaById(int id) {
        String hql = "from Linea where id=" + id;
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return (Linea) query.list().get(0);
    }
   
    @Override
    @Transactional
    public List<Linea> getLineasByPedido(Pedido pedido){
        String hql = "from Linea where pedido="+pedido;
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return query.list();
    }
}
